'''游戏控制面板/提示信息模块'''
import pygame
from game_items import *

class HUDPanel(object):
    """所有面板精灵的控制类"""
    record_filename="record.txt"
    margin=10              #精灵之间的间距
    white = (255,255,255) #白色
    gray = (64,64,64)    # 灰色

    reward_score = 50000    #奖励分值
    level_2_score= 10000    #级别 2 分值
    level_3_score= 10001   #级别 3 分值

    def __init__(self,display_gruop):

        #基本数据
        self.score = 0 #游戏得分
        self.lives_count = 3 #生命记数
        self.level = 1  #游戏级别
        self.best_score = 0  #最好成绩


        # 图像精灵
        # 状态按钮
        self.status_sprite=StatusButton(('pause_nor.png','resume_nor.png'),display_gruop)
        self.status_sprite.rect.topleft=(self.margin,self.margin)

        # 炸弹精灵
        self.bomb_sprite=GameSprite('bomb.png',0,display_gruop)
        self.bomb_sprite.rect.x=self.margin
        self.bomb_sprite.rect.bottom= SCREEN_RECT.bottom-self.margin
    # 炸弹记数标签
        self.bomb_label = Label('×3',32,self.gray,display_gruop)
        self.bomb_label.rect.midleft = (self.bomb_sprite.rect.right+self.margin,
                                        self.bomb_sprite.rect.centery)
    # 生命记数标签
        self.lives_label = Label('×%d'%self.lives_count,32,self.gray,display_gruop)
        self.lives_label.rect.midright=(SCREEN_RECT.right - self.margin,
                                        self.bomb_label.rect.centery)
        # 生命精灵
        self.lives_sprite=GameSprite('life.png',0,display_gruop)
        self.lives_sprite.rect.right=self.lives_label.rect.x-self.margin
        self.lives_sprite.rect.bottom=SCREEN_RECT.bottom-self.margin

    # 得分标签
        self.score_label = Label('%d'%self.score,32,self.gray,display_gruop)
        self.score_label.rect.midleft=(self.status_sprite.rect.right+self.margin,
                                       self.status_sprite.rect.centery)


    # 最好成绩标签
        self.best_label = Label('Best:%d'%self.best_score,36,self.white)
        self.best_label.rect.center=SCREEN_RECT.center


    # 状态标签
        self.status_label = Label('Game Paused',48,self.white)
        self.status_label.rect.midbottom=(self.best_label.rect.centerx,
                                          self.best_label.rect.y- 2*self.margin)

    # 提示标签
        self.tip_label = Label('Press spacebar to continue',22,self.white)
        self.tip_label.rect.midtop =(self.best_label.rect.centerx,
                                     self.best_label.rect.bottom+8*self.margin)

        #从文件里加载最好成绩
        self.load_best_score()
        print(f'best score:{self.best_score}')

    def show_bomb(self,count):
        """修改炸弹数量为 x count"""
        self.bomb_label.set_text('×%d'%count)
        self.bomb_label.rect.midleft = (self.bomb_sprite.rect.right+self.margin,
                                        self.bomb_sprite.rect.centery)

    def show_lives(self):
        """显示最新的生命为 x live_count"""
        self.lives_label.set_text('×%d'%self.lives_count)
        #修正生命计数精灵的位置
        self.lives_label.rect.midright=(SCREEN_RECT.right - self.margin,
                                        self.bomb_label.rect.centery)
        #修正生命精灵的位置
        self.lives_sprite.rect.right=self.lives_label.rect.x-self.margin

    def increase_score(self, enemy_score):
        """增加得分， 注意同时要处理增加生命、关卡升级、更新最好成绩"""

        #计算最新得分
        score = self.score + enemy_score
        # 判断是否增加生命
        if score // self.reward_score != self.score // self.reward_score:
            self.lives_count += 1
            self.show_lives()



        # 更新最好成绩
        self.best_score=score if score>self.best_score else self.best_score
        self.score = score
        #计算新关卡等级
        if score<self.level_2_score:
            level = 1
        elif score <self.level_3_score:
            level = 2
        else:
            level =3


        is_upgrade = level != self.level
        self.level=level

        # 更新得分精灵显示内容
        self.score_label.set_text('%d'%score)
        self.score_label.rect.midleft=(self.status_sprite.rect.right+self.margin,
                                       self.status_sprite.rect.centery)

        # 返回是否升级游戏主逻辑
        return is_upgrade

    def save_best_score(self):
        """将最好得分写入 record.txt"""
        file=open(self.record_filename,"w")
        file.write('%d'%self.best_score)
        file.close()

    def load_best_score(self):
        """从文件里重新加载最好得分"""
        try:
            #读取文件内容
            file=open(self.record_filename,"r")
            content = file.read()
            file.close()

            #转换内容为数字
            self.best_score=int(content)
        except Exception as bug:
            print(f'{bug},have bug!')

    def panel_parsed(self,is_game_over,display_group):
        """游戏停止，显示提示信息。is_game_over 为 True 则说明游戏结束，为 False 则说明游戏暂停"""
        #判断是否已经显示了提示信息
        if display_group.has(self.best_label,self.status_label,self.tip_label):
            return
        #根据游戏状态生成提示文本
        status = 'Game Over!'if is_game_over else 'Game Paused!'
        tip= 'Press spacebar to '
        tip+='play again.'if is_game_over else 'continue.'

        #修改标签精灵的文本内容
        self.best_label.set_text('Best:%d'%self.best_score)
        self.status_label.set_text(status)
        self.tip_label.set_text(tip)

        #修正标签精灵的位置
        self.best_label.rect.center=SCREEN_RECT.center
        self.status_label.rect.midbottom=(self.best_label.rect.centerx,
                                          self.best_label.rect.y- 2*self.margin)
        self.tip_label.rect.midtop =(self.best_label.rect.centerx,
                                     self.best_label.rect.bottom+8*self.margin)

        #把标签精灵添加的精灵组
        display_group.add(self.best_label,self.status_label,self.tip_label)

        #修改状态按钮
        self.status_sprite.switch_statrs(True)
    def panel_resume(self,display_group):
        """取消停止状态，隐藏提示信息"""
        display_group.remove(self.best_label,self.status_label,self.tip_label)

        self.status_sprite.switch_statrs(False)

    def reset_panel(self):
        """重置面板数据"""
        #重置数据
        self.score=0
        self.lives_count=3

        #重置精灵数据
        self.increase_score(0)
        self.show_bomb(3)
        self.show_lives()



